/**
 * Class OrderUploadModule.
 * 
 * @author Flavio Duboc
 */
OrderUploadModule.inherits(Module);
function OrderUploadModule() {
	// Input file unpload
	this.inputFileModule = new InputFileModule({
		'id' : 'inputFileModule',
		'label' : I18n.get('order.upload'),
		'width' : 'x-large'
	});
	this.inputFileModule.getJQ();
	
	// Search form inputs
	this.inputStartDate = new InputDateModule({
		'id' : 'inputStartDate',
		'label' : I18n.get('start.date')
	});
	this.inputEndDate = new InputDateModule({
		'id' : 'inputEndDate',
		'label' : I18n.get('end.date'),
		'value' : moment().add('years', 1).format('L')
	});
	this.inputReferrer = new InputReferrerModule({
		'id' : 'inputReferrer',
		'label' : I18n.get('entity'),
		'value' : Util.check(UserManager.user) ? UserManager.user.referrer : undefined,
		'rootReferrer' : Util.check(UserManager.user) ? UserManager.user.referrer : undefined
	});
	if (Util.check(EnumManager.MonitoringTaskStatus)) {
		this.inputStatus = new InputSelectModule({
			'id' : 'inputStatus',
			'label' : I18n.get('status'),
			'options' : EnumManager.MonitoringTaskStatus
		});
	}
	
	// Result table
	this.uploadTable = this.initUploadTable();
}

OrderUploadModule.prototype.DEFAULT_UPLOAD_ORDERS_REQUEST_PATH = 'uploadCSVOrder';
OrderUploadModule.prototype.DEFAULT_SEARCH_UPLOAD_TASKS_REQUEST_PATH = 'getOrderTasks';
OrderUploadModule.prototype.DEFAULT_UPLOAD_TASK_REPORT_REQUEST_PATH = 'downloadOrderTaskReport';


OrderUploadModule.prototype.setUploadOrdersRequestPath = function(uploadOrdersRequestPath) {
	this.uploadOrdersRequestPath = uploadOrdersRequestPath;
	if (!Util.checkString(uploadOrdersRequestPath)) {
		this.uploadOrdersRequestPath = this.DEFAULT_UPLOAD_ORDERS_REQUEST_PATH;
	}
	return this;
};

OrderUploadModule.prototype.setSearchUploadTasksRequestPath = function(searchUploadTasksRequestPath) {
	this.searchUploadTasksRequestPath = searchUploadTasksRequestPath;
	if (!Util.checkString(searchUploadTasksRequestPath)) {
		this.searchUploadTasksRequestPath = this.DEFAULT_SEARCH_UPLOAD_TASKS_REQUEST_PATH;
	}
	return this;
};

OrderUploadModule.prototype.setDownloadUploadTaskReportRequestPath = function(downloadUploadTaskReportRequestPath) {
	this.downloadUploadTaskReportRequestPath = downloadUploadTaskReportRequestPath;
	if (!Util.checkString(downloadUploadTaskReportRequestPath)) {
		this.downloadUploadTaskReportRequestPath = this.DEFAULT_UPLOAD_TASK_REPORT_REQUEST_PATH;
	}
	return this;
};

OrderUploadModule.prototype.setParams = function(params) {
	this.setUploadOrdersRequestPath(params.uploadOrdersRequestPath);
	this.setSearchUploadTasksRequestPath(params.searchUploadTasksRequestPath);
	this.setDownloadUploadTaskReportRequestPath(params.downloadUploadTaskReportRequestPath);
	return this;
};

OrderUploadModule.prototype.initUploadTable = function(jQ) {
	var that = this;
	
	// Order upload tasks table
	uploadTable = new TableModule();
	uploadTable.id = 'uploadTable';
	
	// Start date
	var startDateCol = new Column('startDate', I18n.get('start.date'), '10%');
	uploadTable.columnList.push(startDateCol);
	startDateCol.getText = function(task) {
		return moment(task.startDate).format('DD/MM/YYYY HH:mm:ss');
	};
	
	// End date
	var endDateCol = new Column('endDate', I18n.get('end.date'), '10%');
	uploadTable.columnList.push(endDateCol);
	endDateCol.getText = function(task) {
		return (task.endDate == null | task.endDate == '')? '' : moment(task.endDate).format('DD/MM/YYYY HH:mm:ss');
	};
	
	// File name
	uploadTable.columnList.push(new Column('fileName', I18n.get('file.name'), '20%'));
	
	// Status
	var statusCol = new Column('status', I18n.get('status'), '11%');
	uploadTable.columnList.push(statusCol);
	statusCol.getText = function(task) {
		return I18n.get('OrderUploadTaskStatus.' + task.status);
	};

	// Download
	var downloadCol = new Column('download', '', '5%');
	uploadTable.columnList.push(downloadCol);
	downloadCol.getAlt = function(task) {
		return I18n.get('download');
	};
	downloadCol.getText = function(task) {
		if (true) { // TODO task.status == 'AVAILABLE') {
			return $('<i class="icon-download-alt icon"/>').click(function() { that.downloadUploadTaskReport(task.id); });
		}
		// TODO return '';
	};
	
	return uploadTable;
};

OrderUploadModule.prototype.uploadOrders = function() {
	var that = this;
	if (this.inputFileModule.value != '') {
		this.inputFileModule.submit(that.uploadOrdersRequestPath, {
			'referrerId' : this.inputReferrer.value.id
		}, function(result) {
			that.reload();
		});
	}
};

OrderUploadModule.prototype.searchUploadTasks = function() {
	var that = this;
	AjaxManager.getJSON(that.searchUploadTasksRequestPath, {
		'referrerId' : this.inputReferrer.value.id,
		'startDate' : moment(this.inputStartDate.value, 'DD/MM/YYYY').valueOf(),
		'endDate' : moment(this.inputEndDate.value, 'DD/MM/YYYY').valueOf(),
		'status' : this.inputStatus.value
	}, function(result) {
		that.uploadTable.objectMap = new Map();
		if (result.success) {
			that.getJQ().find('.result:first').show();
			$.each(result.tasks, function(id, task) {
				that.uploadTable.objectMap.put(id, task);
			});
			that.uploadTable.objectMap.sort('startDate', true);
		}
		that.uploadTable.buildJQ();
	});
};

OrderUploadModule.prototype.downloadUploadTaskReport = function(taskId) {
	var that = this;
	AjaxManager.download(that.downloadUploadTaskReportRequestPath, {
		'taskId' : taskId
	});
};
